package com.aitaoxue.palx.ui;

import uk.co.senab.photoview.PhotoView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.ApiHttpClient;
import com.aitaoxue.palx.base.BaseActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PhotoActivity extends BaseActivity {

	@InjectView(R.id.img)
	PhotoView img;
	@InjectView(R.id.error_gallery_header)
	RelativeLayout head;
	@InjectView(R.id.error_gallery_tail)
	LinearLayout tail;
	@InjectView(R.id.errorbook_galllery_desc)
	TextView desc;
	@InjectView(R.id.errorbook_gallery_go_back)
	ImageView goBack;

	private float lastX;
	private float lastY;
	private boolean visible = true;

	@Override
	protected boolean hasActionBar() {
		return false;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.single_photo;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.errorbook_gallery_go_back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void initView() {
		goBack.setOnClickListener(this);
	}

	@Override
	public void initData() {
		if (getIntent().getBooleanExtra("fromChat", false)) {
			String image=getIntent().getStringExtra("photo");
			ImageLoader.getInstance().displayImage(
					getIntent().getStringExtra("photo"), img);
		} else {
			desc.setText(getIntent().getStringExtra("desc"));

			ImageLoader.getInstance().displayImage(
					ApiHttpClient.ERROR_BOOK_PREFIX
							+ getIntent().getStringExtra("img_url"), img);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			lastX = ev.getX();
			lastY = ev.getY();

			break;
		case MotionEvent.ACTION_UP:
			float currentX = ev.getX();
			float currentY = ev.getY();

			if (currentX == lastX && currentY == lastY) {
				// Toast.makeText(ViewPagerActivity.this, "µãÁËÎÒÅ¶",
				// Toast.LENGTH_SHORT).show();

				if (visible) {
					head.setVisibility(View.GONE);
					tail.setVisibility(View.GONE);
				} else {
					head.setVisibility(View.VISIBLE);
					tail.setVisibility(View.VISIBLE);
				}

				visible = !visible;

			}

			break;
		default:
			break;
		}

		return super.dispatchTouchEvent(ev);
	}

}
